package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.aspact.RecordTimeAnno;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import com.itheima.utils.TliasException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 员工管理
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmpAndExprList(Emp emp) throws TliasException {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        emp.setPassword("123456");
        this.save(emp);

        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        empExprMapper.insert(exprList);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmpAndExprList(List<Integer> ids) throws TliasException {
        this.removeByIds(ids);
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Emp::getId, ids);
        empExprMapper.deleteById(queryWrapper);
    }
    @Override
    public Emp getEmpAndExprListById(Integer id){
        Emp emp = this.getById(id);
        LambdaQueryWrapper<EmpExpr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId, id);
        List<EmpExpr> exprList = empExprMapper.selectList(queryWrapper);
        emp.setExprList(exprList);
        return emp;
    }
    @Override
    public void updateEmpAndExprList(Emp emp) throws TliasException {
        emp.setUpdateTime(LocalDateTime.now());
        this.updateById(emp);
        LambdaQueryWrapper<EmpExpr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId, emp.getId());
        empExprMapper.deleteById(queryWrapper);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        empExprMapper.insert(exprList);
    }
    /*@Override
    public PageResult page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        //1. 设置PageHelper分页参数
        PageHelper.startPage(page, pageSize);
        //2. 执行查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        //3. 封装分页结果
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResult(p.getTotal(), p.getResult());
    }*/

//    public PageResult page(EmpQueryParam empQueryParam) {
//        //1. 设置PageHelper分页参数
//        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
//        //2. 执行查询
//        List<Emp> empList = empMapper.list(empQueryParam);
//        //3. 封装分页结果
//        Page<Emp> p = (Page<Emp>) empList;
//        return new PageResult(p.getTotal(), p.getResult());
//    }
//
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public void save(Emp emp) throws Exception {
//        emp.setCreateTime(LocalDateTime.now());
//        emp.setUpdateTime(LocalDateTime.now());
//        emp.setPassword("123456");
//
//        empMapper.save(emp);
//
//        List<EmpExpr> exprList = emp.getExprList();
//        if (exprList != null && exprList.size() > 0) {
//            exprList.forEach(empExpr ->
//                    empExpr.setEmpId(emp.getId()));
//            empExprMapper.insertBatch(exprList);
//        }
//    }
//    @RecordTimeAnno
//    @Transactional
//    @Override
//
//    public void deleteByIds(List<Integer> ids) {
//        //1. 根据ID批量删除员工基本信息
//        empMapper.deleteByIds(ids);
//
//        //2. 根据员工的ID批量删除员工的工作经历信息
//        empExprMapper.deleteByEmpIds(ids);
//    }
//
//    @Override
//    public Emp getInfo(Integer id) {
//        // 查询员工的基本信息
//        Emp emp = empMapper.getByEmpId(id);
//
//        // 根据员工的id查询工作经历
//        List<EmpExpr> exprList = empExprMapper.getByEmpId(id);
//
//        emp.setExprList(exprList);
//
//        return emp;
//
//    }
//    @RecordTimeAnno
//    @Transactional
//    @Override
//    public void update(Emp emp) {
//        //1. 根据ID更新员工基本信息
//        emp.setUpdateTime(LocalDateTime.now());
//
//        empMapper.updateById(emp);
//
//        //2. 根据员工ID删除员工的工作经历信息 【删除老的】
//        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
//
//        //3. 新增员工的工作经历数据 【新增新的】
//        Integer empId = emp.getId();
//        List<EmpExpr> exprList = emp.getExprList();
//        if (!CollectionUtils.isEmpty(exprList)) {
//            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
//            empExprMapper.insertBatch(exprList);
//        }
//    }
//
//
    @Autowired
    private EmpMapper mapper;

    @Override
    public JobOption getEmpJobData() {

        List<Map> mapList = mapper.getEmpJobData();
        List jobList = new ArrayList();
        List dataList = new ArrayList();
        JobOption jobOption = new JobOption();
        for (Map map : mapList) {
            jobList.add(map.get("jobName"));
            dataList.add(map.get("total"));
        }
        jobOption.setJobList(jobList);
        jobOption.setDataList(dataList);

        return jobOption;
    }
    @Override
    public List<Map> getEmpGenderData() {
        return this.baseMapper.countEmpGenderData();
    }
//
//    @Override
//    public List<Emp> listAll() {
//        return empMapper.listAll();
//    }
//
    @Override
    public LoginInfo login(Empoto empoto) {
      LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(Emp::getUsername, empoto.getUsername());
      queryWrapper.eq(Emp::getPassword, empoto.getPassword());
      Emp emp = this.getOne(queryWrapper);
        if (emp == null) {
            throw new TliasException("用户名或密码错误");
        }


        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId(emp.getId());
        loginInfo.setUsername(emp.getUsername());
        loginInfo.setName(emp.getName());

        Map<String,Object> claims = new HashMap<>();
        claims.put("userId",emp.getId());
        claims.put("name",emp.getName());
        String token = JwtUtils.generateJwt(claims);
        loginInfo.setToken(token);

        return loginInfo;
    }


}